package com.windupurnomo.util;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.windupurnomo.entity.Condition;
import com.windupurnomo.entity.Day;
import com.windupurnomo.entity.Player;
import com.windupurnomo.entity.Resource;
import com.windupurnomo.entity.Task;
import com.windupurnomo.entity.TaskDay;

public class MyQuery {
	
	public static final class TasksDayDAO{
		public static final List<TaskDay> getTasks(int day, SQLiteDatabase db){
			List<TaskDay> lt = new ArrayList<TaskDay>();
			String q = "SELECT * FROM "+D.table.TASK_DAY+" WHERE "+D.field_task_day.ID_DAY+" = "+day;
			Cursor cursor = db.rawQuery(q, null);
			/*id_day, id_task, start, finish*/
			if(cursor.moveToFirst()){
				int idTask = cursor.getInt(1);
				Task t = TaskDAO.getTask(idTask, db);
				Day d = DayDAO.getDay(day, db);
				TaskDay taskD = new TaskDay();
				taskD.setStart(cursor.getInt(2));
				taskD.setFinish(cursor.getInt(3));
				taskD.setTask(t);
				taskD.setDay(d);
				lt.add(taskD);
				while(cursor.moveToNext()){
					idTask = cursor.getInt(1);
					t = TaskDAO.getTask(idTask, db);
					d = DayDAO.getDay(day, db);
					taskD = new TaskDay();
					taskD.setStart(cursor.getInt(2));
					taskD.setFinish(cursor.getInt(3));
					taskD.setTask(t);
					taskD.setDay(d);
					//Log.i("acmWindu", taskD.getTask().getTask());
					lt.add(taskD);
				}
			}
			cursor.close();
			return lt;
		}
	}
	
	public static final class GameDAO{
		public static final List<Integer> getGameDays(int idGame, SQLiteDatabase db){
			List<Integer> listDay = new ArrayList<Integer>();
			String q = "SELECT "+D.field_game.DAY+" FROM "+D.table.GAME+" WHERE "+D.field_game.ID+" = "+idGame;
			Cursor cursor = db.rawQuery(q, null);
			while(cursor.moveToNext()){
				int day = cursor.getInt(0);
				Log.i("acmWindu", "Id day: "+day);
				listDay.add(day);
			}
			cursor.close();
			return listDay;
		}
	}
	
	
	public static final class TaskDAO{
		/*id, task, duration, place, water, food, paper, power, social, finance, eiq*/
		public static final Task getTask(int idTask, SQLiteDatabase db){
			Task task = new Task();
			String q = "SELECT * FROM "+D.table.TASK+" WHERE "+D.field_task.ID+" ="+idTask;
			Cursor cursor = db.rawQuery(q, null);
			if(cursor.moveToFirst()){
				task.setId(idTask);
				task.setTaskName(cursor.getString(1));
				task.setDuration(cursor.getInt(2));
				task.setPlace(cursor.getInt(3));
				Resource r = new Resource();
				r.setWater(cursor.getInt(4));
				r.setFood(cursor.getInt(5));
				r.setPaper(cursor.getInt(6));
				Condition c = new Condition();
				c.setPower(cursor.getInt(7));
				c.setSocial(cursor.getInt(8));
				c.setFinance(cursor.getInt(9));
				c.setEiq(cursor.getInt(10));
				task.setResource(r);
				task.setCondition(c);
			}
			cursor.close();
			return task;
		}
	}
	
	public static final class DayDAO{
		public static final Day getDay(int idDay, SQLiteDatabase db){
			Day d = new Day();
			String q = "SELECT * FROM "+D.table.DAY+" WHERE "+D.field_day.ID+" ="+idDay;
			Cursor cursor = db.rawQuery(q, null);
			if(cursor.moveToFirst()){
				d.setWeather(cursor.getInt(1));
				d.setStart(cursor.getInt(2));
				d.setFinish(cursor.getInt(3));
			}
			return d;
		}
	}
	
	public static final class PlayerDAO{
		public static final void updatePlayer(Player p, SQLiteDatabase db){
			String q = "UPDATE "+D.table.PLAYER+" SET "+
			D.field_player.RES_WATER+" = "+p.getResource().getWater()+","+
			D.field_player.RES_FOOD+" = "+p.getResource().getFood()+","+
			D.field_player.RES_PAPER+" = "+p.getResource().getPaper()+","+
			D.field_player.CON_POWER+" = "+p.getCondition().getPower()+","+
			D.field_player.CON_SOCIAL+" = "+p.getCondition().getSocial()+","+
			D.field_player.CON_FINANCE+" = "+p.getCondition().getFinance()+","+
			D.field_player.CON_EIQ+" = "+p.getCondition().getEiq()+","+
			D.field_player.SCORE+" = "+p.getScore()+
			" WHERE "+D.field_player.ID+ " = "+p.getId()+";";
			db.execSQL(q);
		}
		public static final List<Player> best5(SQLiteDatabase db){
			List<Player> lp = new ArrayList<Player>();
			String q = "SELECT * FROM "+D.table.PLAYER+" ORDER BY "+D.field_player.SCORE+" DESC LIMIT 5";
			Cursor cursor = db.rawQuery(q, null);
			while(cursor.moveToNext()){
				Player p = new Player();
				p.setId(cursor.getInt(0));
				p.setName(cursor.getString(1));
				p.setSex(cursor.getString(2));
				//resource
				int water = cursor.getInt(3);
				int food = cursor.getInt(4);
				int paper = cursor.getInt(5);
				//condition
				int power = cursor.getInt(6);
				int social = cursor.getInt(7);
				int finance = cursor.getInt(8);
				int eiq = cursor.getInt(9);
				Resource r = new Resource(water, food, paper);
				Condition c = new Condition(power, social, finance, eiq);
				p.setResource(r);
				p.setCondition(c);
				p.setScore(cursor.getInt(10));
				lp.add(p);
			}
			return lp;
		}
	}
	
}
